package cn.tedu.luban.worker.main.repository;

import cn.tedu.luban.worker.main.converter.WorkerAreaConverter;
import cn.tedu.luban.worker.main.mapper.WorkerAreaMapper;
import cn.tedu.luban.worker.protocol.bo.WorkerAreaBO;
import cn.tedu.luban.worker.protocol.dos.WorkerAreaDO;
import cn.tedu.luban.worker.protocol.param.WorkerRegistParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class WorkerAreaRepoImpl implements WorkerAreaRepository{

    @Autowired(required = false)
    private WorkerAreaMapper workerAreaMapper;

    @Autowired
    private WorkerAreaConverter workerAreaConverter;


    @Override
    public void deleteByUserId(Long userId) {
        workerAreaMapper.deleteByUserId(userId);
    }

    @Override
    public void saveWorkerAreas(WorkerRegistParam param) {

        // 对象的转换 param -> do
        List<WorkerAreaDO> dos = workerAreaConverter.params2dos(param.getWorkerRegistAreaParams(),param.getUserId());

        // 批量新增
        workerAreaMapper.batchInsert(dos);
    }

    @Override
    public List<WorkerAreaBO> findAreas(Long userId) {
        List<WorkerAreaDO> dos= workerAreaMapper.selectByUserId(userId);
        return workerAreaConverter.dos2bos(dos);
    }

}
